<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sale
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Sale extends CI_Controller {

    //put your code here


    function sale() {
        parent :: __construct();
        $this->load->model('pickup_model');
        $this->load->model('listing_model');
    }

    function set_common_data() {
        $data['car_make'] = $this->pickup_model->get_car_makes();
        $data['car_make_year'] = $this->pickup_model->populate_year('P', 15, date('Y'));
        $data['car_owner'] = $this->pickup_model->get_pickup(9);
        $data['car_country'] = $this->pickup_model->populate_country();
        $data['car_tax'] = $this->pickup_model->get_pickup(10);
        $data['car_insurance'] = $this->pickup_model->get_pickup(11);
        $data['car_insurance_month'] = $this->pickup_model->get_pickup(12);
        $data['car_insurance_year'] = $this->pickup_model->populate_year('N', 20, date('Y') - 5);
        return $data;
    }

    function car() {
        $data = $this->set_common_data();
        $this->load->view('car_sale', $data);
    }

    function submit() {
       $this->form_validation->set_rules('car_make', 'Error! Car Make', 'trim|required');
        $this->form_validation->set_rules('car_model', 'Error! Car Model', 'trim|required');
        $this->form_validation->set_rules('car_version', 'Error! Car Version', 'trim|required');

        $this->form_validation->set_rules('car_make_year', 'Error! Make Year', 'trim|required');
        $this->form_validation->set_rules('car_owner', 'Error! Car Owner', 'trim|required');
        $this->form_validation->set_rules('car_color', 'Error! Car Color', 'trim|required');

        $this->form_validation->set_rules('car_km_done', 'Error! Kilomiter done', 'trim|required|numeric');
        $this->form_validation->set_rules('car_reg_no', 'Error! Registration No', 'trim|required');
        $this->form_validation->set_rules('car_reg_at', 'Error! Registerd at', 'trim|required');

        $this->form_validation->set_rules('car_country', 'Error! Country', 'trim|required');
        $this->form_validation->set_rules('car_state', 'Error! State', 'trim|required');
        $this->form_validation->set_rules('car_city', 'Error! City', 'trim|required');

        $this->form_validation->set_rules('car_insurance_year', 'Error! Insurance Valid upto (Year)', 'trim|required');
        $this->form_validation->set_rules('car_tax', 'Error! Car Tax', 'trim|required');
        $this->form_validation->set_rules('car_insurance', 'Error! Car Insurance', 'trim|required');

        $this->form_validation->set_rules('car_price', 'Error! Expected Price', 'trim|required|numeric');

        $this->form_validation->set_rules('car_insurance_month', 'Error! Insurance Valid upto (Month)', 'trim|required');
        $this->form_validation->set_rules('car_comments', 'Error! Comments', 'trim|required|min_length[5]');
        $this->form_validation->set_rules('car_agree', 'Error! Check Agreement', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data = $this->set_common_data();
            $this->load->view('car_sale', $data);
        } else {
            $post_data = array(
                'type' => 'CAR',
                'make' => $_POST['car_make'],
                'model' => $_POST['car_model'],
                'version' => $_POST['car_version'],
                'make_year' => $_POST['car_make_year'],
                'owner' => $_POST['car_owner'],
                'color' => $_POST['car_color'],
                'km_done' => $_POST['car_km_done'],
                'reg_no' => $_POST['car_reg_no'],
                'reg_at' => $_POST['car_reg_at'],
                'country' => $_POST['car_country'],
                'state' => $_POST['car_state'],
                'city' => $_POST['car_city'],
                'insurance' => $_POST['car_insurance'],
                'insurance_year' => $_POST['car_insurance_year'],
                'insurance_month' => $_POST['car_insurance_month'],
                'tax' => $_POST['car_tax'],
                'price' => $_POST['car_price'],
                'comments' => $_POST['car_comments'],
                'agree' => $_POST['car_agree'],
                'posted_on' => date('Y-M-d', now())
            );

            $msg = $this->listing_model->add($post_data);
            $this->session->set_flashdata('message', $msg);
            redirect('sale/member_dashboard');
        }
        
    }
    function member_dashboard()
    {
     print "Thank You! Your listing has beeen sucessfully inserted in our database......";
    }


}

?>
